<?php
/*
Template Name: Checkout Page
*/

get_header();
$active_option = get_option('azio_style_option', 'option_not_set');
if (have_posts()): the_post();
    $permalink = get_permalink();
    $title = get_the_title();
    ?>

    <div id="main-container" class="<?php echo $active_option->main_container; ?>">
        <div class="page-top-leader omega<?php echo ' '.$active_option->page_top_leader; ?>">
            <div class="container_12 relatived clearfix">
                <h1 class="page-title"><?php echo $title; ?></h1>
                <?php
                if (function_exists('yoast_breadcrumb')) {
                    yoast_breadcrumb('<div class="breadcrumb">', '</div>');
                }
                ?>
            </div>
        </div>
        <!-- .page-top-leader -->
        <div class="container_12">
            <div class="aside-container grid_9">

                <form name="order-form" method="get" id="order-form" action="#">
                    <div class="checkout-tab">
                        <div class="checkout-heading">Thông tin
                            <i class="icomoon edit-tab" aria-hidden="true" data-icon="&#x2199;"></i></div>
                        <div class="checkout-content clearfix">
                            <div class="grid_4">
                                <h3>Thông tin cá nhân</h3>
                                <label>Họ tên: (*)</label>
                                <br>
                                <input type="text" name="cart_name" minlength="2" required placeholder="" value="">
                                <br>
                                <label>E-Mail: (*)</label>
                                <br>
                                <input type="text" name="cart_email" placeholder="" value="">
                                <br>
                                <label>Số điện thoại: (*)</label>
                                <br>
                                <input type="text" name="cart_telephone" minlength="7" placeholder="" value="">
                            </div>
                            <div class="grid_4 omega">
                                <h3>Địa chỉ giao hàng</h3>
                                <label>Địa chỉ: (*)</label>
                                <br>
                                <input type="text" name="cart_address" minlength="7" placeholder="" value="">
                                <br>
                                <label>Ghi chú:</label>
                                <br>
                                <textarea name="cart_comment" style="width: 95%; height: 116px;"></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- end .checkout-tab -->
                    <div class="checkout-tab">
                        <div class="checkout-heading">Xác nhận đơn hàng
                            <i class="icomoon edit-tab" aria-hidden="true" data-icon="&#x2199;"></i></div>
                        <div class="checkout-content checkout-product clearfix">
                            <?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) { ?>
                                <table>
                                    <thead>
                                    <tr>
                                        <td class="image">Hình ảnh</td>
                                        <td class="name">Tên sản phẩm</td>
                                        <td class="quantity">Số lượng</td>
                                        <td class="price">Đơn giá (VNĐ)</td>
                                        <td class="total">Tổng cộng</td>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach ($_SESSION['cart'] as $cart_product) {
                                        ?>
                                        <tr id="cart-product-<?php echo $cart_product['id']; ?>">
                                            <td class="image">
                                                <a href="<?php echo $cart_product['link']; ?>" title="<?php echo esc_attr($cart_product['name']); ?>">
                                                    <img src="<?php echo $cart_product['image']; ?>" alt="<?php echo esc_attr($cart_product['name']); ?>" width="100" height="100"/>
                                                </a>
                                            </td>
                                            <td class="name"><?php echo $cart_product['name']; ?></td>
                                            <td class="quantity"><?php echo $cart_product['num']; ?></td>
                                            <td class="price"><?php echo number_format($cart_product['price'], 0, ',', ','); ?>
                                                ₫
                                            </td>
                                            <td class="total"><?php echo number_format(($cart_product['price'] * $cart_product['num']), 0, ',', ','); ?>
                                                ₫
                                            </td>
                                        </tr>
                                    <?php
                                    } // endforeach
                                    ?>
                                    </tbody>
                                    <tfoot>
                                    <?php
                                    if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
                                        $cart_sum = cal_total_price($_SESSION['cart']);
                                    } else {
                                        $cart_sum['total'] = 0;
                                    } ?>
                                    <tr>
                                        <td colspan="3">Tổng cộng</td>
                                        <td colspan="2"><?php echo number_format($cart_sum['total'], 0, ',', ','); ?> ₫</td>
                                    </tr>
                                    </tfoot>
                                </table>
                            <?php } // endif ?>
                        </div>
                    </div>
                    <!-- checkout-tab -->
                    <button type="submit" class="button style-button right">Xác nhận đặt hàng</button>
            </div>
            </form>
            <script type="text/javascript">
                jQuery(document).ready(function ($) {
                    jQuery("#order-form").validate({
                        rules: {
                            cart_name: {
                                required: true,
                                minlength: 2
                            },
                            cart_email: {
                                required: true,
                                email: true
                            },
                            cart_telephone: {
                                required: true,
                                minlength: 7
                            },
                            cart_address: {
                                required: true,
                                minlength: 2
                            }
                        },
                        messages: {
                            cart_name: {
                                required: "Vui lòng điền họ tên",
                                minlength: "Vui lòng điền họ tên đầy đủ"
                            },
                            cart_email: "Địa chỉ email không hợp lệ",
                            cart_telephone: {
                                required: "Vui lòng điền số điện thoại liên lạc",
                                minlength: "Số điện thoại không hợp lệ"
                            },
                            cart_address: {
                                required: "Vui lòng điền địa chỉ nhận hàng",
                                minlength: "Địa chỉ không hợp lệ"
                            }
                        },
                        submitHandler: function (form) {
                            var action = $("<input type='hidden' value='azio_ajax_checkout' name='action'/>");
                            $(form).append(action);
                            var options = {
                                url: azio_ajax.ajaxurl,
                                type: "POST",
                                dataType: 'json',
                                //clearForm : true,
                                resetForm: true,
                                beforeSubmit: function (formData, jqForm, options) {
                                    $('input, button[type=submit]', $(form)).attr('disabled', true).css({'opacity': '0.5'});
                                },
                                success: function (data, statusText, xhr, $form) {

                                    alert(data.message);

                                    $('input, button[type=submit]', $(form)).attr('disabled', false).css({'opacity': 1});
                                    action.remove();
                                }
                            };
                            $(form).ajaxSubmit(options);

                            return false;
                        }
                    });
                });
            </script>

            <?php// get_sidebar('product') ?>
            <!-- end #column-left -->

        </div>
        <!-- end .container_12 -->

        <?php get_template_part('block', 'baseinfo') ?>
        <!-- end .base-info -->
    </div>
    <!-- end #main-container -->

<?php endif ?>

<?php get_footer() ?>